<?php

namespace app\apishop\controller;

use \think\Request;
use \think\Db;
use think\Session;
use think\Config;
use think\Controller;
use think\Validate;
use think\Cookie;
use think\Cache;
use app\apishop\controller\Base;

//----------------------------------------------
// 食材供应商APP端接口
//----------------------------------------------
class Apishop extends Base {

    /**
     * @writer:yangjian
     * @DateTime:2018-01-11
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: Apishop测试接口
     * @return [type]
     */
    public function index() {
        return '食材供应商APISHOP接口模块运行正常!';
    }

    /*     * ***************************************** */
    //商户注册登录信息
    /*     * ***************************************** */

    /**
     * @writer:曾林
     * @DateTime:2018-01-15
     * @rewriter:杨建
     * @rewriter_date:2018-01-17
     * @description:食材供应商登录接口
     * @param: tel：手机号 || pwd:密码 || code:验证码 || type:APP/WEB 登录
     * @return[$result]
     */
    public function login() {
        $type = input('type');
        $tel = input('tel');
        $password = input('pwd');
        if (empty($password) || empty($tel)) {
            $result = Config::get('code.miss');
            return json($result);
        }
        if ($password) {
            $where['password'] = md5($password);
        }
        $where['account'] = $tel;
        $Re = db('supplier')->where($where)->find();
        $Res = Db::name('system_setting')->select();
        if (db('supplier')->where(['account' => $tel])->find()) {
            if ($Re) {
                Session::set('register_tel', $tel);
                Cookie::set('user', $Re, 7 * 24 * 60 * 60);
                $token = md5(time() . rand(10000, 99999));
                $arr   = [];
                if ($type == 'APP') {
                    $arr['app_token'] = $token;
                }
                if ($type == 'WEB') {
                    $arr['web_token'] = $token;
                }
                //添加token
                db('supplier')->where(['account' => $tel])->update($arr);
                $Ret = Db::name('supplier')
                        ->alias('A')
                        ->field('A.*,S.name as disName,S.id as dis_id,B.message')
                        ->join('discount S', 'S.supplier_id = A.id', 'left')
                        ->join('supplier_auth B', 'B.supplier_id = A.id', 'left')
                        ->where('A.account', $tel)
                        ->order('dis_id desc')
                        ->find();
                $Ret['minimum'] = number_format($Ret['minimum'], 2, '.', '');
                $result = Config::get('code.success');
                $noticeRead = Db::table('notice_read')->where(['type'=>1,'status'=>'read'])->count();
                $result['noticeRead'] = $noticeRead;
                $result['setting'] = $Res;
                $result['info'] = $Ret;
            } else {
                $result = Config::get('code.cpNoMate');
                $result['info'] = '';
            }
        } else {
            $result = Config::get('code.notreg');
        }
        return json($result);
    }

    /**
     * @writer:曾林
     * @DateTime:2018-01-17
     * @description:食材供应商登录接口
     * @param:code:验证码 || type:APP/WEB 登录
     * @return[$result]
     */
    public function codeLogin() {
        $type = input('type');
        $tel = input('tel');
        $code = input('code');
        //$str = "zyq878hj25jk8kdjj55jkal55j4kaskdj";
        if (empty($code) || empty($tel)) {
            $result = Config::get('code.miss');
            return json($result);
        }
        $where['account'] = $tel;
        $Re = db('supplier')->where($where)->find();
        $Res = Db::name('system_setting')->select();
        if (db('supplier')->where(['account' => $tel])->find()) {
            if ($Re) {
                //查到数据库有此号码，跳到账号密码登录
                if ($code == Session::get('telCode_' . $tel)) {
                    Cookie::set('user', $Re, 30 * 24 * 60 * 60);
                    Session::set('register_tel', $tel);
                    $token = md5(getRndString(32));
                    $arr = [];
                    if ($type == 'APP') {
                        $arr = array('app_token' => $token);
                    }
                    if ($type == 'WEB') {
                        $arr = array('web_token' => $token);
                    }
                    //添加token
                    db('supplier')->where(['account' => $tel])->update($arr);
                    // $result = Config::get('code.success');
                    // $result['info'] = $Re;
                    // $result['setting'] = $Res;
                    $Ret = Db::name('supplier')
                            ->alias('A')
                            ->field('A.*,S.name as disName,S.id as dis_id,B.message')
                            ->join('discount S', 'S.supplier_id = A.id', 'left')
                            ->join('supplier_auth B', 'B.supplier_id = A.id', 'left')
                            ->where('A.account', $tel)
                            ->order('dis_id desc')
                            ->find();
                    $Ret['minimum'] = number_format($Ret['minimum'], 2, '.', '');
                    $result = Config::get('code.success');
                    $noticeRead = Db::table('notice_read')->where(['type'=>1,'status'=>'read'])->count();
                    $result['noticeRead'] = $noticeRead;
                    $result['setting'] = $Res;
                    $result['info'] = $Ret;
                } else {
                    $result = Config::get('code.code');
                }
            } else {
                $result = Config::get('code.tel');
            }
        } else {
            $result = Config::get('code.notreg');
        }
        return json($result);
    }

    /**
     * @writer:曾林
     * @DateTime:2018-01-17
     * @description:获取用户协议
     * @return[$result]
     */
    public function getAgreementData() {
        try {
            $data = db::table('protocol')->where(['type' => 2])->find();
            if ($data) {
                $result = Config::get('code.success');
                $result['data'] = $data;
            } else {
                $result = Config::get('code.error');
            }
        } catch (Exception $e) {
            $result['msg'] = $e->getMessage();
            //exit();
        }
        return json($result);
    }

    /**
     * @writer:曾林
     * @DateTime:2018-01-17
     * @description:获取商户二维码
     * @return[$result]
     */
    public function getCodeImage() {
        try {
            $data = db::table('system_setting')->where(['name' => '商户二维码'])->find();
            $data['ios_image'] = db::table('system_setting')->where(['name' => '商户ios二维码'])->value('value');
            if ($data) {
                $result = Config::get('code.success');
                $result['data'] = $data;
            } else {
                $result = Config::get('code.error');
            }
        } catch (Exception $e) {
            $result['msg'] = $e->getMessage();
            //exit();
        }
        return json($result);
    }

    /**
     * @writer:曾林
     * @DateTime:2018-01-15
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description:食材供应商注册接口
     * @param:[]
     * @return[$result]
     */
    public function register() {
        $password = input('pwd');
        $tel = input('tel');
        $code = input('code');
        $agreement = input('is');
        if ($code == Session::get('telCode_' . $tel)) {
            $arr['password'] = md5($password);
            $arr['account'] = $tel;
            $arr['agreement'] = $agreement;
            $arr['registration_time'] = date("Y-m-d H:i:s");
            $arr['app_token'] = randomNum();
            //$arr['code'] = $code;
            $re = validate('Rules')->scene('register')->check($arr); //验证字段
            if (true == $re) {
                if (db('supplier')->where(['account' => $tel])->find()) {
                    $result = Config::get('code.regin');
                } else {
                    if (db('supplier')->insert($arr)) {
                        //添加商户到商量浏览表
                        $supplier_id = db::name('supplier')->where(['account' => $tel])->value('id');
                        db::name('supplier_browse')->insert(['supplier_id' => $supplier_id, 'browse_num' => 1]);
                        Session::set('register_tel', $tel);
                        $result = Config::get('code.success');
                        //$result['web_token']  = $arr['app_token'];
                    } else {
                        $result = Config::get('code.error');
                    }
                }
            } else {
                $result = Config::get('code.mistake');
            }
        } else {
            $result = Config::get('code.code');
        }
        return json($result);
    }

    /**
     * @writer:曾林
     * @DateTime:2018-01-17T15:52:00+0800
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description:完善供应商信息
     * @param:
     */
    public function setSupplierInfo() {
        $post = request()->post();
        // $id = $post['supplier_id'];
        $arr['name'] = $post['company'];
        $arr['company'] = $post['company'];
        $arr['manager'] = $post['manager'];
        $arr['tel'] = $post['tel'];
        $arr['post_code'] = $post['post_code'];
        $arr['company_code'] = $post['company_code'];
        $arr['address'] = $post['address'];
        $data = Db::table('gb_postal_code')->where(['postal_code'=>['like',$post['post_code'].'%']])->find();
        $arr['admin_name1'] = $data['admin_name1'];
        $arr['admin_name2'] = $data['admin_name2'];
        $arr['admin_name3'] = $data['admin_name3'];
        $arr['check_status'] = 1;
        $Res = validate('Rules')->scene('setSupplierInfo')->check($arr); //验证字段
        if (!$Res) {
            $result = Config::get('code.mistake');
            return json($result);
        }
        $where['account'] = Session::get('register_tel');
        $whereD['id'] = input('id');
        $Re = Db::name('supplier')->where($where)->whereOr($whereD)->update($arr);
        if ($Re !== false) {
            $Re = Db::name('supplier')->where($where)->find();
            $result = Config::get('code.success');
            $result['info'] = $Re;
        } else {
            $result = Config::get('code.null');
        }
        return json($result);
    }

    /**
     * @writer:曾林
     * @DateTime:1018-1-15 17:20
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description:食材供应商重置密码
     * @param:
     * @return [$result]
     */
    public function resetPassword() {
        $id = input('id');
        $password = md5(input('pwd'));
        $new_password = md5(input('new_pwd'));
        if ($password == "") {
            $result = Config::get('code.null');
        } else {
            //验证原密码
            if (!db('supplier')->where(['id' => $id, 'password' => $password])->find()) {
                $result = Config::get('code.mistake');
            }
            //修改密码
            else {
                $data['password'] = $new_password;
                if (db('supplier')->where(['id' => $id])->update($data)) {
                    $result = Config::get('code.success');
                } else {
                    $result = Config::get('code.error');
                }
            }
        }
        return json($result);
    }

    /**
     * @writer:曾林
     * @DateTime:1018-1-15 14:20
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description:食材供应商重置手机号
     * @param:
     * @return [$result]
     */
    public function resetTel() {
        $id = input('id');
        $tel = input('tel');
        if ($tel == "") {
            $result = Config::get('code.null');
        } else {
            //验证原手机号
            if (!db('supplier')->where(['account' => $tel])->find()) {
                //修改手机号
                $data['tel'] = $tel;
                $data['account'] = $tel;
                if (db('supplier')->where(['id' => $id])->update($data)) {
                    $result = Config::get('code.success');
                } else {
                    $result = Config::get('code.error');
                }
            } else {
                $result = Config::get('code.tel');
            }
        }

        return json($result);
    }

    /**
     * @writer:曾林
     * @DateTime:1018-1-16 10:20
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description:食材供应商找回密码
     * @param:
     * @return [$result]
     */
    public function findPassword() {
        $tel = input('tel');
        $password = md5(input('pwd'));
        $code = input('code');
        //验证手机号
        $data = Db::table('supplier')->where(['account' => $tel])->find();
        if (!$data) {
            $result = Config::get('code.null');
        }
        //找回密码
        else {
            if ($code == Session::get('telCode_' . $tel)) {
                if ($password == $data['password']){
                    $result = array('status' => 200, 'msg' => '操作成功');
                }else{
                    $re = Db::table('supplier')->where('account',$tel)->setField('password', $password);
                    if ($re) {
                        $result = array('status' => 200, 'msg' => '操作成功');
                    } else {
                        $result = Config::get('code.error');
                    }
                }
            } else {
                $result = Config::get('code.code');
            }
        }
        return json($result);
    }

    /**
     * @writer:曾林
     * @DateTime:2018-01-16
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 获取通知信息接口
     * @param:
     * @return [type]
     */
    public function getNotice() {
        $where['n.supplier_id'] = input('supplier_id');
        $supplier_id = input('supplier_id');
        $where['n.show_type'] 	= 2;
        $where['r.type'] 		= 1;
        $whereOr = "n.notice_type = 3 and n.supplier_id is null and n.show_type in (2,3) and n.status = 1 and r.type = 1 and r.user_id = $supplier_id";
        if (input('status')) {
            $where['r.status'] = input('status'); //状态搜索
            $status = input('status');
            $whereOr = "n.notice_type = 3 and n.supplier_id is null and n.show_type in (2,3) and n.status = 1 and r.type = 1 and r.user_id = $supplier_id and r.status = '$status'";
        }
        $data = Db::table('notice')
				->alias('n')
				->join("notice_read r", "r.notice_id = n.id", 'left')
                ->where($where)
                ->whereOr($whereOr)
                ->field('n.*,r.*,n.id as notice_id')
                ->order('n.create_time desc')
                ->paginate(10, false, ['path' => 'javascript:AjaxPage([PAGE])','var_page'=>'page_no']);
        if ($data) {
            $result = Config::get('code.success');
            $result['count'] = $data->toArray()['total'];
            $result['list'] = $data->toArray()['data'];
            $result['maxPage'] = $data->toArray()['last_page'];
        } else {
            $result = Config::get('code.null');
        }
        return json($result);
    }

    /**
     * @writer:曾林
     * @DateTime:2018-01-16
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 获取通知信息详情接口
     * @param:
     * @return [type]
     */
    public function getNoticeInfo() {
        $id     = input('id');
        if ($id) {
            $data = db('notice')->where(['id' => $id])->find();
            if ($data) {
                $result = Config::get('code.success');
                $result['info'] = $data;
                //改变状态值
                db('notice_read')->where('notice_id',$id)->update(['status' => 'read']);
            } else {
                $result = Config::get('code.null');
            }
        } else {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    public function getNoticeList() {
        $post = Request::instance()->post();
        if (!empty($post['supplier_id']) && !empty($post['page_no']) && !empty($post['$page_size'])) {
            $supplier_id = $post['supplier_id'];
            $pageNo      = $post['page_no'];
            $pageSize    = $post['$page_size'];
            $end_time    = date("Y-m-d", time());
            $date        = explode('-', $end_time);
            $start_time  = date("Y-m-d", mktime(0, 0, 0, $date[1], $date[2] - 32, $date[0]));
            $end_time    = date("Y-m-d", mktime(0, 0, 0, $date[1], $date[2] + 1, $date[0]));
            $data = Db::table('notice')
                ->alias('n')
                ->field('n.*,n.id as notice_id')
                ->where(['n.supplier_id'  =>$supplier_id,'n.show_type'=>2])
                ->whereOr(function ($query) {
                    $query->where('n.supplier_id', null)->where('n.show_type',2);
                })
                ->whereTime('n.create_time', '>', $start_time)
                ->whereTime('n.create_time', '<', $end_time)
                ->limit(($pageNo-1)*$pageSize,$pageSize)
                ->order('n.create_time desc')
                ->select();
            $count = count($data);
            if ($data) {
                $result = Config::get('code.success');
                $result['list']     = $data;
                $result['count']    = $count;
            } else {
                $result = Config::get('code.null');
            }
        }else{
            $result = Config::get('code.mistake');
        }
        return json($result);
    }


    /**
     * @writer:曾林
     * @DateTime:2018-01-16
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description:删除通知信息接口
     * @param:id:获取信息id主键
     * @return [type]
     */
    public function delNotice() {
        $id = input('id', 0, 'intval');
        if ($id) {
            $data = db('notice_read')->where(['id' => $id])->delete();
            if ($data) {
                $result = Config::get('code.success');
            } else {
                $result = Config::get('code.error');
            }
        } else {
            $result = Config::get('code.mistake');
        }

        return json($result);
    }

    /**
     * @writer:曾林
     * @DateTime:2018-01-16
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 店铺开关
     * @param: is 判断是否开启：1开启，0不开启
     * @return [result]
     */
    public function setOpen() {
        $id = input('id');
        $is = input('is');
        $data = [];
        if (0 == $is) {
            $data['is_open'] = 0;
        } else if (1 == $is) {
            $data['is_open'] = 1;
        }
        if (db('supplier')->where(['id' => $id])->update($data)) {
            $result = Config::get('code.success');
        } else {
            $result = Config::get('code.error');
        }
        return json($result);
    }

    /**
     * @writer:曾林
     * @DateTime:2018-01-17
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 供应商信息接口
     * @param：根据用户cookie回去用户id
     * @return [result]
     */
    public function shopInfo() {
        $id = input('supplier_id', 0, 'intval');
        if (!$id) {
            $result = Config::get('code.logout');
        } else {
            $data = db('supplier')->where(['id' => $id])->find();
            if ($data) {
                $result = array('status' => 200, 'msg' => '操作成功', 'data' => $data);
            } else {
                $result = Config::get('code.null');
            }
        }
        return json($result);
    }

    /**
     * @writer:wangyufei
     * @DateTime:2018-01-17
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 满送商品分类
     * @return [result]
     */

    /**
     * 商户满送商品选择数据
     * @return type
     */
    public function getProductGivezTree() {
        try {
            $id = input('supplier_id');
            $where = ['p.supplier' => $id, 'p.status' => 1, 'p.is_shelves' => 1, 'ps.is_default' => 1];
            $goods = Db::name('product')
                ->alias('p')
                ->field('p.*, ps.price')
                ->join('product_specification ps','p.id = product_id')
                ->where($where)
                ->group('p.id')
                ->select();
            foreach ($goods as $k=>$v){
                $goods[$k]['price'] = number_format($v['price'], 2, '.', '');
            }
            //获取商家商品所有分类id
            $ids = self::getCategoryIds($goods);
            //根据分类id组装ztree数据
            $ztree = self::getCategoryzTree($ids, $goods);
            $res = $ztree;
            if (empty($res)) {
                $res = config('code.null');
            }
        } catch (\Exception $ex) {
            $res = config('code.lose');
            $res['data'] = [$ex->getMessage()];
        }
        return json($res);
    }

    /**
     * @writer:曾林
     * @DateTime:2018-01-17
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 供应商信息接口
     * @param:商户信息
     * @return [result]
     */
    public function shopUpdate() {
        $data = Request::instance()->post();
        $arr = [];
        if (!empty($data)){
            $id                         = $data['supplier_id'];
            $arr['name']                = $data['name'];
            $arr['image']               = $data['img'];
            $arr['manager_tel']         = $data['tel'];
            $arr['manager_tel2']        = $data['tel2'];
            $arr['manager_tel3']        = $data['tel3'];
            $arr['business']            = $data['business'];
            $arr['shop_introduction']   = $data['shop_introduction'];
            $res = Db::table('supplier')->where('id',$id)->update($arr);
            if ($res){
                $result = Config::get('code.success');
            }else{
                $result = Config::get('code.error');
            }
        }else{
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * @writer:曾林
     * @DateTime:2018-01-17
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 客服接口
     * @param:根据区域id：area 选取客服
     * @return [result]
     */
    public function service() {
        $id = input('area');
        if (!$id) {
            $result = Config::get('code.null');
        } else {
            $data = db('customer_service_district')
                    ->alias('a')
                    ->join('customer_service w', 'a.district_id = w.id')
                    ->where(['customer_service_id' => $id])
                    ->find();
            if ($data) {
                $result = array('status' => 200, 'msg' => '操作成功', 'data' => $data);
            } else {
                $result = Config::get('code.null');
            }
        }
        return json($result);
    }

    /**
     * @writer:yangjian
     * @DateTime:2018-01-16T17:27:08+0800
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description:删除配送地址
     * @param:
     * @param:
     */
    function delAddr() {
        $post = request()->post();
        $tag = $post['tag'];
        $day = $post['day'];
        $sign = $post['sign'];
        $id = $post['supplier_id'];
        if (empty($id) || empty($tag) || empty($day)) {
            $result = Config::get('code.miss');
            return json($result);
        }
        $delwhere['send_area'] = $post['name'];
        $delwhere['day'] = $day;
        $delwhere['supplier_id'] = $id;
        if (input('month')) {
            $delwhere['month'] = $post['month'];
        }
        $Res = Db::name('supplier_send_manage')
                ->where($delwhere)
                ->delete();
        if ($Res !== false) {
            $result = Config::get('code.delsuccess');
        } else {
            $result = Config::get('code.error');
        }
        $result['where'] = $delwhere;
        return json($result);
    }

    /**
     * @writer:曾林
     * @DateTime:2018-01-17
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 清除登录缓存
     * @param:
     * @return [result]
     */
    public function logout() {
        Cookie::delete('user');
        $result = Config::get('code.success');
        return json($result);
    }

    /**
     * 获取商品基础数据ids
     * @param array $arr
     * @return array
     */
    protected static function getCategoryIds(array $arr) {
        $ids = [];
        foreach ($arr as $v) {
            $ids[] = $v['category_id'];
            $ids[] = $v['type_id'];
            $ids[] = $v['small_type_id'];
        }
        return array_unique($ids);
    }

    /**
     * 获取用户token
     * @param array $arr
     * @return array
     */
    public function getUserToken(){
        $data = Request::instance()->post();
        $web_token = Db::table('supplier')->where('id',$data['supplier_id'])->value('web_token');
        if ($web_token){
            $result['status']     = 200;
            $result['web_token']  = $web_token;
        }else{
            $result = Config::get('code.null');
        }
        return json($result);
    }
    /**
     * 获取分类基础数据
     */
    public function getBaseData(){
        $ids = input();//Request::instance()->post();
        //$ids = input('1,2,3');
        $where['parent_id'] = array('in',$ids['ids']);
        $where['flag'] = array('eq',1);
        $data = Db::table('base_data')->where($where)->select();
        if ($data){
            $result['status']     = 200;
            $result['data']  = $data;
        }else{
            $result = Config::get('code.null');
        }
        return json($result);
    }


    /**
     * 分类信息ztree结构组装
     * @param array $ids
     * @param array $goods
     * @return array
     */
    protected static function getCategoryzTree(array $ids, array $goods) {
        $where['id'] = ['in', $ids];
        $data = Db::name('base_data')->where($where)->field("id,concat_ws('/',name,name_english)as name ,parent_id as children,image,parent_id as pid")->select();
        $tree = self::getTree($data);
        $ztree = self::getzTree($goods, $tree);
        return $ztree;
    }

    /**
     * ztree数据组装
     * @param array $data
     * @param type $pid
     * @return array
     */
    protected static function getTree(array $data, $pid = 0) {
        $tree = [];
        foreach ($data as $v) {
            if ($v['children'] == $pid) {
                $v['children'] = self::getTree($data, $v['id']);
                $v['isParent'] = TRUE;
                $tree[] = $v;
            }
        }
        return $tree;
    }

    /**
     * 将商品数据添加进分类ztree中
     * @param array $data
     * @param array $tree
     * @return array
     */
    protected static function getzTree(array $data, array $tree) {
        $arr = [];
        foreach ($tree as $k => $v) {
            if (!empty($v['children'])) {
                foreach ($data as $key => $val) {
                    if (!$val['small_type_id'] && $val['type_id'] == $v['id']) {
                        $val['isParent'] = false;
                        $val['children'] = [];
                        $v['children'][] = $val;
                        $tree[$k]['children'][] = $v;
                    } elseif (!$val['small_type_id'] && !$val['type_id'] && $val['category_id'] == $v['id']) {
                        $val['isParent'] = false;
                        $val['children'] = [];
                        $tree[$k]['children'][] = $val;
                    } else {
                        $tree[$k]['children'] = self::getzTree($data, $v['children']);
                    }
                }
            } elseif ($v['isParent']) {
                foreach ($data as $key => $val) {
                    if ($val['small_type_id'] == $v['id'] || $val['type_id'] == $v['id'] || $val['category_id'] == $v['id']) {
                        $tree[$k]['isParent'] = false;
                        $arr[] = $val;
                    }
                }
                $tree[$k]['children'] = $arr;
                $arr = [];
            }
        }
        return $tree;
    }

    public function getDatabase() {
        $where['parent_id'] = 0;
        $where['type_menu'] = 1;
        $where['level'] = 0;
        $where['flag'] = 1;
        $data = Db::name('base_data')   // 一级数据
                ->where($where)
                ->field("id,concat_ws('/',name,name_english) as name ,image,parent_id as pid")
                ->select();
//        foreach ($data as $k1 => $v1) {
//            $a['parent_id'] = $v1['id'];
//            $res1 = Db::name('base_data')   //二级
//                    ->where($a)
//                    ->field("id,concat_ws('/',name,name_english) as name ,image,parent_id as pid")
//                    ->select();
//            if (!empty($res1)) {
//                foreach ($res1 as $k2 => $v2) {
//                    $b['parent_id'] = $v2['id'];
//                    $res2 = Db::name('base_data')   //三级
//                            ->where($b)
//                            ->field("id,concat_ws('/',name,name_english) as name ,image,parent_id as pid")
//                            ->select();
//                    if (!empty($res2)) {
//                        $res1[$k2]['isParent'] = TRUE;
//                        $res1[$k2]['children'] = $res2;
//                    }
//                }
//                $data[$k1]['isParent'] = TRUE;
//                $data[$k1]['children'] = $res1;
//            }
//        }
        return json($data);
    }

    /**
     * @writer:wuqiubai
     * @DateTime:2018-03-22
     * @rewriter:xx
     * @rewriter_date:
     * @description: 小货郎 数据
     * @param:[]
     * @return[$result]
     */
    public function defaults(){
        $xhlData = [];
        $xhlData['tel']                = Db::table('system_setting')->where('name','平台电话')->value('value');
        $xhlData['email']              = Db::table('system_setting')->where('name','平台邮箱')->value('value');
        $xhlData['addr']               = Db::table('system_setting')->where('name','地址')->value('value');
        $xhlData['addr_english']       = Db::table('system_setting')->where('name','地址_英文')->value('value');
        $xhlData['manager']            = Db::table('system_setting')->where('name','联系人')->value('value');
        $xhlData['manager_english']    = Db::table('system_setting')->where('name','联系人_英文')->value('value');
        $xhlData['image']              = Db::table('system_setting')->where('name','用户二维码')->value('value');
        $xhlData['content']            = Db::table('protocol')->where('type',4)->value('content');
        $xhlData['supplier_content']   = Db::table('protocol')->where('type',2)->value('content');
        if ($xhlData){
            $result['status']     = 200;
            $result['xhlData']    = $xhlData;
        }else{
            $result = Config::get('code.null');
        }
        return json($result);
    }


}
